package fhq.hcmute.edu.vn.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import fhq.hcmute.edu.vn.adapter.MySpinnerAdapter;
import fhq.hcmute.edu.vn.util.BundleUtil.Search;
import fhq.hcmute.edu.vn.util.ProjectUtil;
import fhq.hcmute.edu.vn.util.SearchUtil;

public class SearchRemoteFileActivity extends Activity {

	private Button btnOK;
	private Button btnCancel;
	private TextView txtKeywords;
	private Spinner spCategory;
	private Spinner spPath;
	private String[] fileType;
	private MySpinnerAdapter fileTypeAdapter;
	private MySpinnerAdapter pathAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_remote_file);
		init();
	}

	private void init() {
		btnOK = (Button) findViewById(R.id.btnOK);
		btnOK.setOnClickListener(onOKClick);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(onCancelClick);
		spCategory = (Spinner) findViewById(R.id.spCategory);
		spCategory.setOnItemSelectedListener(onFileTypeCategory);
		spPath = (Spinner) findViewById(R.id.spPath);
		txtKeywords = (TextView) findViewById(R.id.txtKeywords);
		setupSpinnerCategory();
		setupSpinnerPath();
	}

	private OnClickListener onOKClick = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			Bundle bundle = new Bundle();
			bundle.putStringArray(Search.FILE_TYPE, fileType);
			bundle.putString(Search.FOLDER_NAME, spPath.getSelectedItem()
					.toString());
			bundle.putString(Search.KEY_WORD, txtKeywords.getText()
					.toString());
			Intent data = new Intent();
			data.putExtras(bundle);
			setResult(ProjectUtil.RESULT_CODE_SEARCH_FILE, data);
			finish();
		}
	};
	private OnClickListener onCancelClick = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}
	};

	private void setupSpinnerPath() {
		Bundle bundle = getIntent().getExtras();
		List<String> list = bundle.getStringArrayList(Search.LIST_PATH);
		pathAdapter = new MySpinnerAdapter(SearchRemoteFileActivity.this, list);
		pathAdapter.add(0, getString(R.string.Label_Search_Path_Current));
		pathAdapter.setDropDownViewResource(
				android.R.layout.simple_spinner_dropdown_item);
		spPath.setAdapter(pathAdapter);
	}

	private void setupSpinnerCategory() {
		List<String> list = new ArrayList<String>();
		for (String str : getResources().getStringArray(R.array.file_type_list)) {
			list.add(str);
		}
		fileTypeAdapter =
				new MySpinnerAdapter(SearchRemoteFileActivity.this, list);
		fileTypeAdapter.setDropDownViewResource(
				android.R.layout.simple_spinner_dropdown_item);
		spCategory.setAdapter(fileTypeAdapter);
	}

	@Override
	protected void onActivityResult(int requestCode,
			int resultCode, Intent data) {
		if (requestCode == ProjectUtil.REQUEST_CODE_SPECIFIC_TYPE) {
			if (resultCode == ProjectUtil.RESULT_CODE_SPECIFIC_TYPE) {
				String specificType =
						data.getStringExtra(SearchSpecificFileActivity.SPECIFIC_TYPE);
				fileTypeAdapter.add(specificType);
				fileTypeAdapter.notifyDataSetChanged();
				spCategory.setSelection(fileTypeAdapter.getCount() - 1);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private OnItemSelectedListener onFileTypeCategory =
			new AdapterView.OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int position, long arg3) {
					switch (position) {
					case SearchUtil.ALL_TYPE:
						fileType = SearchUtil.allType;
						break;
					case SearchUtil.AUDIO:
						fileType = SearchUtil.audio;
						break;
					case SearchUtil.COMPRESS:
						fileType = SearchUtil.compress;
						break;
					case SearchUtil.IMAGE:
						fileType = SearchUtil.image;
						break;
					case SearchUtil.VIDEO:
						fileType = SearchUtil.video;
						break;
					case SearchUtil.TEXT:
						fileType = SearchUtil.text;
						break;
					case SearchUtil.SPECIFIC:
						Intent intent = new Intent(getApplicationContext(),
								SearchSpecificFileActivity.class);
						startActivityForResult(intent,
								ProjectUtil.REQUEST_CODE_SPECIFIC_TYPE);
						break;
					default:
						fileType = new String[] { spCategory.getSelectedItem()
								.toString() };
						break;
					}
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {

				}

			};
}
